const utils = require("../utils/utils.js");
const toast = require("../utils/toasts.js");
const comReq = require("../utils/commonRequest.js");
import {
  upload_image_only,
  card_update_share_img,
  req
} from "../utils/api.js";

const wxm = require("../utils/wechat_modules.js");

const promisify = require('../utils/promisify');
const wxCanvasToTempFilePath = promisify(wx.canvasToTempFilePath);
const wxSaveImageToPhotosAlbum = promisify(wx.saveImageToPhotosAlbum);
const wxGetImageInfo = promisify(wx.getImageInfo);

exports.cusTypeStr = function(type) {
  if (type == 'QZ') {
    return '新客户';
  } else if (type == 'Z') {
    return '有效客户';
  } else if (type == 'YY') {
    return '预约客户';
  } else if (type == 'CJ') {
    return '成交客户';
  } else if (type == 'WX') {
    return '无效客户';
  }
  return '新客户';
}

exports.cusType = function(type) {
  if (type == '新客户') {
    return 'QZ';
  } else if (type == '新客户') {
    return 'Z';
  } else if (type == '预约客户') {
    return 'YY';
  } else if (type == '成交客户') {
    return 'CJ';
  } else if (type == '无效客户') {
    return 'WX';
  }
  return '新客户';
}

exports.reserveStatusStr = function(type) {
  if (type == 'CREATE') {
    return '已预约';
  } else if (type == 'DEAL') {
    return '已成交';
  } else if (type == 'UNDEAL') {
    return '未成交';
  } else if (type == 'EXPIRED') {
    return '已完成';
  } else if (type == 'CANCEL') {
    return '已取消';
  }
  return '';
}

exports.msgTypeStr = function(type) {
  if (type == 'LM') {
    return '给你留言：';
  } else if (type == 'EX_CARD') {
    return '请求与你交换名片';
  } else if (type == 'USEROP') {
    return '';
  } else if (type == 'RESERVE') {
    return '预约';
  } else if (type == 'CANCELRESERVE') {
    return '取消了预约';
  } else if (type == 'NEWCUS') {
    return '你有新客户了，快点查看';
  } else if (type == 'ORDER') {
    return '订单状态变更';
  } else if (type == 'SYS') {
    return '系统通知';
  }
  return '通知';
}

exports.agentLevelStr = function(type) {
  if (type == 1) {
    return '省级代理';
  } else if (type == 2) {
    return '市级代理';
  } else if (type == 3) {
    return '区县代理';
  } else if (type == 4) {
    return '高级代理';
  } else if (type == 5) {
    return '初级代理';
  }
  return '非代理';
}

exports.agentLevelStrArray = function() {

  return ['省级代理', '市级代理', '区县代理', '高级代理', '初级代理'];
}

exports.agentLevel = function(type) {
  if (type == '省级代理') {
    return 1;
  } else if (type == '市级代理') {
    return 2;
  } else if (type == '区县代理') {
    return 3;
  } else if (type == '高级代理') {
    return 4;
  } else if (type == '初级代理') {
    return 5;
  }
  return '';
}

exports.agentStatusStr = function(type) {
  if (type == 'REQUEST') {
    return '申请中';
  } else if (type == 'SUCCESS') {
    return '已通过';
  } else if (type == 'FAIL') {
    return '未通过';
  }
  return '已取消';
}


exports.sourceTypeStr = function(type) {
  if (type == 'USER') {
    return '来自用户推荐';
  } else if (type == 'SALES') {
    return '来自我发出的名片';
  } else if (type == 'XCX') {
    return '来自平台推荐';
  }
  return '来自平台推荐';
}

exports.roleTypeStr = function(type) {
  if (type == 'COMMON') {
    return '普通员工';
  } else if (type == 'MANAGER') {
    return '部门管理员';
  } else if (type == 'BOSS') {
    return '公司管理员';
  }
  return '普通员工';
}

exports.vipStr = function(type, withOp) {
  var str = '非VIP';
  var op = '(点击开通)';
  if (type == 'SINGLE') {
    str = '个人版VIP';
    op = '(点击续费)';
  } else if (type == 'MULTI') {
    str = '企业版VIP';
    op = '(点击续费)';
  } else if (type == 'TRY') {
    str = '试用版';
    op = '(开通正式版)';
  } else {
    str = '非VIP';
    op = '(点击开通)';
  }

  return str + (withOp ? op : '');
}

exports.isVip = function(type) {
  return (type == 'SINGLE') || type == 'MULTI' || type == 'TRY';
}

exports.opTypeStr = function(type) {
  if (type == 'VIEW') {
    return '查看';
  } else if (type.indexOf('COPY') >= 0) {
    return '复制';
  } else if (type == 'SHARE') {
    return '转发';
  } else if (type == 'PRAISE') {
    return '点赞';
  } else if (type == 'FAV') {
    return '收藏';
  } else if (type == 'PHONE_CALL') {
    return '拨打';
  } else if (type == 'LM') {
    return '给你留言了';
  } else if (type == 'RESERVE') {
    return '预约';
  } else if (type == 'CONSULT') {
    return '咨询';
  } else if (type == 'VIEW_LIST') {
    return '查看';
  } else if (type == 'RECEIVE_COUPON') {
    return '领取';
  } else if (type == 'OPENMAP') {
    return '导航'
  }
  return '操作';
}

exports.opObjTypeStr = function(type, opType) {
  if (opType == 'LM') {
    return null;
  }

  if (type == 'SELLER') {
    return '你公司';
  } else if (type == 'PRODUCT') {
    return '你推荐的商品';
  } else if (type == 'SALES') {
    return '你';
  } else if (type == 'WORKS') {
    return '你的作品';
  } else if (type == 'COUPON') {
    return '你公司的优惠券';
  } else if (type == 'TIMELINE') {
    return '你公司的动态';
  }
  return '你';
}

exports.opObjStr = function(type, opType) {
  if (opType == 'LM') {
    return null;
  } else if (opType == 'PHONE_CALL') {
    return '电话';
  } else if (opType == 'COPY_WX') {
    return '微信';
  } else if (opType == 'COPY_QQ') {
    return 'QQ';
  } else if (opType == 'COPY_EMAIL') {
    return '邮箱';
  } else if (opType == 'COPY_PHONE') {
    return '手机号';
  } else if (opType == 'COPY_ADDRESS') {
    return '地址';
  } else if (opType == 'COPY') {
    return '信息';
  } else if (opType == 'RESERVE') {
    return '回访';
  } else if (opType == 'OPENMAP') {
    return '地址';
  }

  if (type == 'SELLER') {
    return '公司官网';
  } else if (type == 'PRODUCT' || type == 'WORKS' || type == 'COUPON' || type == 'TIMELINE') {
    return '';
  } else if (type == 'SALES') {
    return '名片';
  }
  return '名片';
}

exports.getOpTypeFromStr = function(opType) {
  if (opType == '电话') {
    return 'PHONE_CALL';
  } else if (opType == '微信') {
    return 'COPY_WX';
  } else if (opType == 'QQ') {
    return 'COPY_QQ';
  } else if (opType == '邮箱') {
    return 'COPY_EMAIL';
  } else if (opType == '手机号') {
    return 'COPY_PHONE';
  } else if (opType == '地址') {
    return 'COPY_ADDRESS';
  } else if (opType == '联系电话') {
    return 'COPY_PHONE';
  }

  return 'COPY';
}

exports.getVerifyStatusStr = function(type) {
  if (type == 'REQUEST') {
    return '认证中';
  } else if (type == 'SUCCESS') {
    return '已认证';
  } else if (type == 'FAIL') {
    return '认证失败';
  }

  return '未认证';
}

exports.orderStatusStr = function(type) {
  if (type == 'CREATE') {
    return '待支付';
  } else if (type == 'CANCEL') {
    return '已取消';
  } else if (type == 'PAY') {
    return '已支付订金';
  } else if (type == 'PTING') {
    return '拼团中';
  } else if (type == 'PTFAIL') {
    return '拼团失败 已退款';
  } else if (type == 'FINISH') {
    return '已完成';
  } else if (type == 'TAIL_PAY') {
    return '已支付尾款';
  } else if (type == 'JX_1') {
    return '当前进度科目一';
  } else if (type == 'JX_2') {
    return '当前进度科目二';
  } else if (type == 'JX_3') {
    return '当前进度科目三';
  } else if (type == 'JX_4') {
    return '当前进度科目四';
  } else if (type == 'JX_JZ') {
    return '当前进度已拿到驾照';
  } else if (type == 'RATE') {
    return '已评价';
  } else if (type == 'EXPIRED') {
    return '已过期';
  } else if (type == 'REFUND') {
    return '已退款';
  }

  return '';
}

exports.orderStatusOpStr = function(type) {
  if (type == 'CREATE') {
    return '去支付';
  } else if (type == 'PAY') {
    return '支付尾款';
  } else if (type == 'JX_JZ' || type == 'FINISH') {
    return '服务评价';
  } else if (type == 'RATE') {
    return '查看评价';
  }

  return '联系驾校';
}

exports.payTypeStr = function(type) {
  if (type == 'GIVE') {
    return '赠送';
  } else if (type == 'ALI') {
    return '支付宝';
  } else if (type == 'WX') {
    return '微信支付';
  } else if (type == 'CASH') {
    return '现金';
  } else if (type == 'ONLINE_BANK') {
    return '银行转行';
  } else if (type == 'SCORE') {
    return '积分';
  } else if (type == 'BALANCE') {
    return '账户余额';
  } else if (type == 'OFFLINE') {
    return '线下付款';
  }

  return '其他';
}

exports.payType = function(type) {
  if (type == '赠送') {
    return 'GIVE';
  } else if (type == '支付宝') {
    return 'ALI';
  } else if (type == '微信支付') {
    return 'WX';
  } else if (type == '现金') {
    return 'CASH';
  } else if (type == '银行转行') {
    return 'ONLINE_BANK';
  } else if (type == '积分') {
    return 'SCORE';
  } else if (type == '账户余额') {
    return 'BALANCE';
  } else if (type == '线下付款') {
    return 'OFFLINE';
  }

  return 'OTHER';
}

exports.disStr = function(dis) {
  if (dis >= 1000) {
    var num = dis / 1000;
    return num.toFixed(1) + 'km';
  } else if (dis >= 100) {
    return dis + 'm';
  }

  return '<100m';
}

exports.statusStrArray = function(type) {
  return ['新客户', '有效客户', '预约客户', '成交客户', '无效客户'];
}

exports.statusArray = function(type) {
  return ['QZ', 'Z', 'YY', 'CJ', 'WX'];
}

exports.fuTypeStr = function(type) {
  if (type == 'FU') {
    return '跟进进展';
  } else if (type == 'MDR') {
    return '修改预计成交率';
  } else if (type == 'MDD') {
    return '修改预计成交时间';
  } else if (type == 'TOV') {
    return '修改为有效客户';
  } else if (type == 'TOUV') {
    return '修改为无效客户';
  } else if (type == 'TOR') {
    return '修改为预约客户';
  } else if (type == 'TON') {
    return '修改为新客户';
  } else if (type == 'TOD') {
    return '修改为成交客户';
  } else if (type == 'ADDTAG') {
    return '添加标签';
  } else if (type == 'REPLY') {
    return '回复留言';
  } else if (type == 'RTOC') {
    return '取消了预约';
  } else if (type == 'RTOD') {
    return '修改预约状态为已成交';
  } else if (type == 'RTOU') {
    return '修改预约状态为未成交';
  } else if (type == 'LM') {
    return '给客户留言';
  }
  return '跟进进展';
}

exports.tagsStr = function(tags, count) {
  if (!tags) {
    return '';
  }
  if (!count) {
    count = 100;
  }

  var ts = tags.split(',');
  var tags = '';
  for (var i = 0; i < count & i < ts.length; i++) {
    if (ts[i] == '') {
      continue;
    }
    tags = tags + '“' + ts[i] + '” ';
  }

  return tags;
}


exports.defaultLogo = function(user) {
  if (user) {
    if (user.logo) {
      return user.logo;
    }
    if (user.sex == 'FEMALE') {
      return 'https://img.lingxing.site/img/default_logo_nv.png';
    }
  }
  return 'https://img.lingxing.site/img/default_nan_logo1.png';
}

exports.introTitle = function(obj) {
  // var t = '你好,我是' + obj.title + obj.realName + ',我擅长' + obj.skills + ',这是我的名片';
  // if (obj.cardType == 'BIZ') {
  //   t = '你好,我是' + obj.company + '的' + obj.title + '' + obj.realName + ',这是我的名片';
  // }
  var t = '您好！这是我的微信智能名片，请您惠存！';
  return t;
}

exports.gotoMap = function(op) {
  // wx.navigateTo({
  //   url: '/pages/map/map?title='+op.title+'&lat='+op.lat+'&lng='+op.lng
  // })
  wx.openLocation({
    latitude: op.lat,
    longitude: op.lng,
    name: op.title,
    address: op.address,
    scale: 15
  })
}
exports.agentAddressStr = function(obj, sep) {
  var se = '';
  if (sep) {
    se = ' ';
  }

  if (obj.agentProvince) {
    var addressStr = obj.agentProvince + (obj.agentCity ? se + obj.agentCity : '') + (obj.agentArea ? se + obj.agentArea : '');
    return addressStr;
  }

  return '';
}

exports.addressStr = function(obj, sep) {
  var se = '';
  if (sep) {
    se = ' ';
  }

  var addressStr = (obj.province ? obj.province : '') + (obj.city ? se + obj.city : '') + (obj.area ? se + obj.area : '') + (obj.address ? se + obj.address : '') + (obj.detailAddress ? se + obj.detailAddress : '');
  obj.addressStr = addressStr;
  return addressStr;

}

exports.addressStr1 = function(obj, sep) {
  var se = '';
  if (sep) {
    se = ' ';
  }

  if (obj.province) {
    var addressStr = (obj.city ? se + obj.city : '') + (obj.area ? se + obj.area : '') + (obj.address ? se + obj.address : '') + (obj.detailAddress ? se + obj.detailAddress : '');
    obj.addressStr = addressStr;
    return addressStr;
  }

  return '';
}

exports.gotoPage = function(path, isRedirect) {
  if (path == '/pages/index/index' || path.indexOf('customerAdmin') != -1 || path == '/pages/message/message' || path == '/pages/personal/personal') {
    wx.switchTab({
      url: path,
    })
  } else {
    if (isRedirect) {
      wx.redirectTo({
        url: path,
      })
    } else {
      wx.navigateTo({
        url: path
      })
    }
  }
}

exports.gotoCard = function(sellerId, salesId) {
  wx.navigateTo({
    url: '/pages/cardDetail/cardDetail?tabIndex=1&id=' + salesId + '&sellerId=' + sellerId,
  })
}

exports.gotoCom = function(sellerId, salesId) {
  wx.navigateTo({
    url: '/pages/cardDetail/cardDetail?tabIndex=2&id=' + salesId + '&sellerId=' + sellerId,
  })
}

exports.gotoPs = function(sellerId, salesId) {
  wx.navigateTo({
    url: '/pages/cardDetail/cardDetail?tabIndex=3&id=' + salesId + '&sellerId=' + sellerId,
  })
}

exports.cardTypeStrs = function() {
  return [
    "个人",
    "公司",
    "商城",
    "公众号"
  ];
}

exports.gzhStatusStrArray = function() {
  return [
    "未留言",
    "已留言",
    "已合作",
    "已拒绝"
  ];
}

exports.cardTypes = function() {
  return ["PER", "BIZ", "SHOP", "GZH"];
}


exports.cardTypeIndex = function(type) {
  var types = this.cardTypes();
  for (var i = 0; i < types.length; i++) {
    if (type == types[i]) {
      return i;
    }
  }

  return 0;
}

exports.cardTypeStrCs = function() {
  return ["智能名片+商城+营销工具", "智能名片+商城+公司官网+营销工具", "商城+营销工具", "主页+商城+营销工具"];
}

exports.rates = function(score) {
  var rates = [0, 0, 0, 0, 0];
  for (var i = 0; i < score; i++) {
    rates[i] = 1;
    if (i + 1 > score) {
      rates[i] = 0.5;
    }
  }

  return rates;
}


exports.parseAddress = function(address) {
  if (!address) {
    return;
  }

  var map = {};
  var pi = address.indexOf('省');
  var ci = address.indexOf('市');

  var start = 0;
  if (pi != -1) {
    var pro = address.substring(0, pi + 1);
    map.province = pro;
    start = pi + 1;
  }
  if (ci != -1) {
    var city = address.substring(start, ci + 1);
    map.city = city;
  }
  return map;
}

exports.rateScore = function(totalRateScore, totalRateCount) {
  var rateScore = 0;
  if (totalRateScore && totalRateCount) {
    rateScore = (totalRateScore / totalRateCount).toFixed(1);
  }
  return rateScore;
}

exports.answerStr = function(answertrue, type) {
  var ans = ['A', 'B', 'C', 'D'];
  if (type == 1) {
    if (answertrue == 1) {
      return '正确';
    } else if (answertrue == 2) {
      return '错误';
    }
  } else if (type == 2) {
    return ans[answertrue - 1];
  } else if (type == 3) {
    var res = '';
    var at = answertrue + '';
    for (var i = 0; i < at.length; i++) {
      res += ans[parseInt(at.substring(i, i + 1)) - 1];
    }
    return res;
  }
  return;
}


exports.getWorks = function(sellerId, that) {
  req('/works/query', {
    sellerId: sellerId,
    pageNo: 1,
    pageSize: 50
  }).then(res => {
    // console.log(res);
    if (res.success) {
      var list = res.result.list;
      if (list) {
        that.setData({
          works: list,
        })
      }
    } else {}
  });
}


exports.previewWorks = function(e,that) {
  var index = e.currentTarget.dataset.index;
  if (!index) {
    index = 0;
  }
  var works = that.data.works;
  var imgs = [];
  for (var i = 0; i < works.length; i++) {
    imgs.push(works[i].images);
  }

  wx.previewImage({
    urls: imgs,
    current: imgs[index]
  })
}